package com.example.demobk.common;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class PasswordUtils {
    public static  String encrypt(String password){
        String salt = IdUtil.simpleUUID();
        //密码md5 盐值加密码
        String finalPasssword = SecureUtil.md5(salt + password);
        return salt+ "$" +finalPasssword;
    }

    //解密
    public  static  boolean dencrypt(String password ,String securePasssword){
        boolean result = false;
        if (StringUtils.hasLength(password) &&(StringUtils.hasLength(securePasssword))){
            if (securePasssword.length() == 65 && securePasssword.contains("$")){
                String[] securePassswordArr = securePasssword.split("\\$");
                //盐值
                String salt = securePassswordArr[0];
                String finalPasssword = securePassswordArr[1];
                password = SecureUtil.md5(salt +password);
                if (finalPasssword.equals(password)){
                    result = true;
                }
            }
        }
        return  result;
    }

}
